package com.bba.manager.user.controller;

import com.bba.manager.common.model.user.Employee;
import com.bba.manager.common.model.user.VO.loginVO;
import com.bba.manager.common.util.JwtUtil;
import com.bba.manager.common.util.ResponseVO;
import com.bba.manager.user.service.ICustomerService;
import com.bba.manager.user.service.IEmployeeService;
import com.bba.manager.user.service.ILoginService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class LoginController {
    @Autowired
    private ILoginService loginService;
    @Autowired
    private IEmployeeService employeeService;
    @PostMapping("/login")
    public ResponseVO login(@RequestParam String employeeId, // 接收前端params中的employeeId
                            @RequestParam String password    , HttpServletResponse response){
        loginVO loginVO=new loginVO();
        loginVO.setUsername(employeeId);
        loginVO.setPassword(password);
        Employee employee = loginService.login(loginVO);
        Map<String,Object> map=new HashMap<>();
        map.put("role",employee.getRole());
        map.put("id",employee.getId());
        String token= JwtUtil.generateToken(map);
        Map<String,String> map1=new HashMap<>();
        map1.put("token",token);
        //将token写入响应头
        response.setHeader("token",token);//将身份认证写入token
        System.out.println(response);
        return ResponseVO.success(map1);
    }
    //返回当前角色所有信息
    @PostMapping("/info")
    public ResponseVO info(@RequestParam String token) {
//        String token = request.getParameter("token");
        Long id = JwtUtil.parseToken(token).get("id", Long.class);
        return ResponseVO.success(employeeService.getById(id));
    }

}
